package com.tangkeyon.controller.manage;

import com.github.pagehelper.PageInfo;
import com.google.common.collect.Maps;
import com.tangkeyon.common.Const;
import com.tangkeyon.common.ResponseCode;
import com.tangkeyon.pojo.Product;
import com.tangkeyon.pojo.User;
import com.tangkeyon.service.IFileService;
import com.tangkeyon.service.IProductService;
import com.tangkeyon.util.PropertiesUtil;
import com.tangkeyon.vo.ProductDetailVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * @Author: tangkeyon@gmail.com
 * @Date: 2019/5/29 14:19
 * @Version 1.0
 */
@Controller
@RequestMapping("/manage/product/")
public class ProductManageController {
    @Autowired
    private IProductService iProductService;
    @Autowired
    protected IFileService iFileService;

    /**
     * @Description: 保存或者修改商品，根据传入的对象是否有id属性进行区别
     * @param session
     * @param product
     * @return
     */
    @RequestMapping(value = {"save_product.do","update_product.do"})
    @ResponseBody
    public ResponseCode saveOrUpdateProduct(HttpSession session, Product product){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if(user !=null){
            if(user.getRole()==Const.Role.ROLE_ADMIN){
                return iProductService.saveOrUpdateProduct(product);
            }
                return ResponseCode.createByErrorMessage("非管理员登录，没有权限操作");
        }else{
            return ResponseCode.createByErrorMessage("未登录，请先登录");
        }

    }

    /**
     * @Description: 修改产品的状态
     * @param session
     * @param productId
     * @param status
     * @return
     */
    @RequestMapping("/set_product_status.do")
    @ResponseBody
    public ResponseCode setProductStatus(HttpSession session,int productId,int status){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if(user !=null){
            if(user.getRole()==Const.Role.ROLE_ADMIN){
                return iProductService.setProductStatus(productId,status);
            }
            return ResponseCode.createByErrorMessage("非管理员登录，没有权限操作");
        }else{
            return ResponseCode.createByErrorMessage("未登录，请先登录");
        }
    }

    /**
     * @Description: 返回商品的详情
     * @param session
     * @param productId
     * @return
     */
    @RequestMapping("/get_detail.do")
    @ResponseBody
    public ResponseCode<ProductDetailVO> getDetail(HttpSession session, int productId){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if(user !=null){
            if(user.getRole()==Const.Role.ROLE_ADMIN){
                return iProductService.manageProductInfo(productId);
            }
            return ResponseCode.createByErrorMessage("非管理员登录，没有权限操作");
        }else{
            return ResponseCode.createByErrorMessage("未登录，请先登录");
        }
    }

    /**
     * 分页显示商品列表
     * @param session
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping("/get_list.do")
    @ResponseBody
    public ResponseCode<PageInfo> getList (HttpSession session, @RequestParam(value = "pageNum",defaultValue = "1")
            int pageNum, @RequestParam(value = "pageSize",defaultValue = "2") int pageSize){
            User user = (User) session.getAttribute(Const.CURRENT_USER);
        if(user !=null){
            if(user.getRole()==Const.Role.ROLE_ADMIN){
                return iProductService.selectProductListByPage(pageNum,pageSize);
            }
            return  ResponseCode.createByErrorMessage("非管理员登录，没有权限操作");
        }else{
            return ResponseCode.createByErrorMessage("未登录，请先登录");
        }
    }

    /**
     * 查询商品并进行分页显示
     * @param session
     * @param pageSize
     * @param pageNum
     * @param productName
     * @param productId
     * @return
     */
    @RequestMapping("/search_product.do")
    @ResponseBody
      public ResponseCode<PageInfo> searchProduct(HttpSession session,
                                                  Integer pageSize,Integer pageNum,
                                                 @RequestParam(required = false) String productName,
                                                  @RequestParam(required = false) Integer productId){
          User user = (User) session.getAttribute(Const.CURRENT_USER);
          if(user !=null){
              if(user.getRole()==Const.Role.ROLE_ADMIN){
                  return iProductService.searchProduct(pageSize,pageNum,productName,productId);
              }
              return ResponseCode.createByErrorMessage("非管理员登录，没有权限操作");
          }else{
              return ResponseCode.createByErrorMessage("未登录，请先登录");
          }
      }
    /**
     * 上传文件暂存在本地，然后上传到ftp服务器上，向前台返回上传文件的uri和URL
     * @param session
     * @param file
     * @param request
     * @return
     */
    @RequestMapping("/upload.do")
    @ResponseBody
      public ResponseCode upload(HttpSession session,@RequestParam(required = false) MultipartFile file, HttpServletRequest request){
          User user = (User) session.getAttribute(Const.CURRENT_USER);
          if(user !=null){
              if(user.getRole()==Const.Role.ROLE_ADMIN){
                  String uploadFilePath = request.getSession().getServletContext().getRealPath("upload");
                  String ftpFilePath= iFileService.upload(uploadFilePath, file);
                  String url= PropertiesUtil.getProperty("ftp.server.http.prefix")+ftpFilePath;
                  Map fileMap= Maps.newHashMap();
                   fileMap.put("uri",ftpFilePath);
                   fileMap.put("url",url);
                   return ResponseCode.creatBySuccess(fileMap);
              }
              return ResponseCode.createByErrorMessage("非管理员登录，没有权限操作");
          }else{
              return ResponseCode.createByErrorMessage("未登录，请先登录");
          }

      }

    /**
     * @Description: 上传富文本
     * @param session
     * @param file
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/richTextUpload.do")
    @ResponseBody
      public Map richTextUpload(HttpSession session, @RequestParam(required = false) MultipartFile file,
                                HttpServletRequest request,
                                HttpServletResponse response){
          Map resultMap=Maps.newHashMap();
          User user = (User) session.getAttribute(Const.CURRENT_USER);
          if(user !=null){
              if(user.getRole()==Const.Role.ROLE_ADMIN){
                  String uploadFilePath = request.getSession().getServletContext().getRealPath("upload");
                  String ftpFilePath= iFileService.upload(uploadFilePath, file);
                  String url= PropertiesUtil.getProperty("ftp.server.http.prefix")+ftpFilePath;
                  resultMap.put("success",false);
                  resultMap.put("file_path",url);
                  return resultMap;
              }
                 resultMap.put("msg","非管理员登录，没有权限操作");
                 resultMap.put("success",false);
          }else{
              resultMap.put("msg","没有登录，请登录");
              resultMap.put("success",false); }
          return null;
      }

}
